﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DB = HTComics.Data;
using HTComics.Web.Common;

namespace HTComics.Web.Admin
{
    public partial class PriceGuide : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            if (Session["IsAdmin"] == null || !Convert.ToBoolean(Session["IsAdmin"]))
            {
                Response.Redirect("Login.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadPriceBands();
        }

        private void LoadPriceBands()
        {
            DataTable dtPriceBands = DB.Misc.GetPriceBands();

            ListItem item = new ListItem();
            item.Text = "Please Select";
            item.Value = "-1";
            ddlPriceBands.Items.Add(item);

            for (int i = 0; i <= dtPriceBands.Rows.Count - 1; i++)
            {
                item = new ListItem();
                item.Text = dtPriceBands.Rows[i]["RangeStart"] + " -- " + dtPriceBands.Rows[i]["RangeEnd"];
                item.Value = dtPriceBands.Rows[i]["BandId"].ToString();
                ddlPriceBands.Items.Add(item);
            }
        }

        private static Dictionary<string, string> GetConditionTypes()
        {
            DataTable dtCondtitionTypes = DB.Misc.GetConditionTypes();
            Dictionary<string, string> cTypes = new Dictionary<string, string>();

            for (int i=0; i <= dtCondtitionTypes.Rows.Count-1; i++)
                cTypes.Add(dtCondtitionTypes.Rows[i]["Code"].ToString(), dtCondtitionTypes.Rows[i]["ConditionId"].ToString());

            return cTypes;
        }

        private void LoadPriceGuide()
        {
            ClearFields();

            if (ddlPriceBands.SelectedValue == "-1")
                return;

            DataTable dtPriceGuide = DB.Misc.GetPriceGuide();
            DataRow[] rows = dtPriceGuide.Select("BandId = " + ddlPriceBands.SelectedValue);

            txtNMP.Value = rows[0]["Factor"].ToString();
            txtNM.Value = rows[1]["Factor"].ToString();
            txtNMM.Value = rows[2]["Factor"].ToString();

            txtVFP.Value = rows[3]["Factor"].ToString();
            txtVFVA.Value = rows[4]["Var_A"].ToString();
            txtVFVB.Value = rows[4]["Var_B"].ToString();
            txtVFM.Value = rows[5]["Factor"].ToString();

            txtFP.Value = rows[6]["Factor"].ToString();
            txtFVA.Value = rows[7]["Var_A"].ToString();
            txtFVB.Value = rows[7]["Var_B"].ToString();
            txtFM.Value = rows[8]["Factor"].ToString();

            txtVGP.Value = rows[9]["Factor"].ToString();
            txtVGVA.Value = rows[10]["Var_A"].ToString();
            txtVGVB.Value = rows[10]["Var_B"].ToString();
            txtVGM.Value = rows[11]["Factor"].ToString();

            txtGP.Value = rows[12]["Factor"].ToString();
            txtGVA.Value = rows[13]["Var_A"].ToString();
            txtGVB.Value = rows[13]["Var_B"].ToString();
            txtGM.Value = rows[14]["Factor"].ToString();

            txtFR.Value = rows[15]["Factor"].ToString();
            txtP.Value = rows[16]["Factor"].ToString();
        }

        private void ClearFields()
        {
            txtNMP.Value = "";
            txtNM.Value = "";
            txtNMM.Value = "";

            txtVFP.Value = "";
            txtVFVA.Value = "";
            txtVFVB.Value = "";
            txtVFM.Value = "";

            txtFP.Value = "";
            txtFVA.Value = "";
            txtFVB.Value = "";
            txtFM.Value = "";

            txtVGP.Value = "";
            txtVGVA.Value = "";
            txtVGVB.Value = "";
            txtVGM.Value = "";

            txtGP.Value = "";
            txtGVA.Value = "";
            txtGVB.Value = "";
            txtGM.Value = "";

            txtFR.Value = "";
            txtP.Value = "";
        }

        protected void ddlPriceBands_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadPriceGuide();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string bandId = ddlPriceBands.SelectedValue;
            Dictionary<string, string> cTypes = GetConditionTypes();

            if (bandId == "-1")
                return;

            DB.Admin.UpdatePriceGuide(cTypes["NM+"], bandId, txtNMP.Value, "", "");
            DB.Admin.UpdatePriceGuide(cTypes["NM"], bandId, txtNM.Value, "", "");
            DB.Admin.UpdatePriceGuide(cTypes["NM-"], bandId, txtNMM.Value, "", "");

            DB.Admin.UpdatePriceGuide(cTypes["VF+"], bandId, txtVFP.Value, "", "");
            DB.Admin.UpdatePriceGuide(cTypes["VF"], bandId, "", txtVFVA.Value, txtVFVB.Value);
            DB.Admin.UpdatePriceGuide(cTypes["VF-"], bandId, txtVFM.Value, "", "");

            DB.Admin.UpdatePriceGuide(cTypes["F+"], bandId, txtFP.Value, "", "");
            DB.Admin.UpdatePriceGuide(cTypes["FN"], bandId, "", txtFVA.Value, txtFVB.Value);
            DB.Admin.UpdatePriceGuide(cTypes["F-"], bandId, txtFM.Value, "", "");

            DB.Admin.UpdatePriceGuide(cTypes["VG+"], bandId, txtVGP.Value, "", "");
            DB.Admin.UpdatePriceGuide(cTypes["VG"], bandId, "", txtVGVA.Value, txtVGVB.Value);
            DB.Admin.UpdatePriceGuide(cTypes["VG-"], bandId, txtVGM.Value, "", "");

            DB.Admin.UpdatePriceGuide(cTypes["GD+"], bandId, txtGP.Value, "", "");
            DB.Admin.UpdatePriceGuide(cTypes["GD"], bandId, "", txtGVA.Value, txtGVB.Value);
            DB.Admin.UpdatePriceGuide(cTypes["GD-"], bandId, txtGM.Value, "", "");

            DB.Admin.UpdatePriceGuide(cTypes["FR"], bandId, txtFR.Value, "", "");
            DB.Admin.UpdatePriceGuide(cTypes["P"], bandId, txtP.Value, "", "");

            /*
             * Update in Application
             * */
            UpdateAppCollections();

            Page.ClientScript.RegisterStartupScript(this.GetType(),"", "<script type='text/javascript'>success();</script>");
        }

        private void UpdateAppCollections()
        {
            PriceBandCollection priceBandColl = new PriceBandCollection();
            PriceGuideCollection priceGuideColl = new PriceGuideCollection();

            DataTable dtPriceBands = DB.Misc.GetPriceBands();
            for (int i = 0; i <= dtPriceBands.Rows.Count - 1; i++)
            {
                priceBandColl.Add(new PriceBand()
                {
                    BandId = Convert.ToInt32(dtPriceBands.Rows[i]["BandId"]),
                    RangeStart = Convert.ToDouble(dtPriceBands.Rows[i]["RangeStart"]),
                    RangeEnd = Convert.ToDouble(dtPriceBands.Rows[i]["RangeEnd"])
                });
            }

            DataTable dtPriceGuide = DB.Misc.GetPriceGuide();
            for (int i = 0; i <= dtPriceGuide.Rows.Count - 1; i++)
            {
                HTComics.Web.Common.PriceGuide guide = new HTComics.Web.Common.PriceGuide();
                guide.ConditionId = Convert.ToInt32(dtPriceGuide.Rows[i]["ConditionId_1"]);
                guide.BandId = Convert.ToInt32(dtPriceGuide.Rows[i]["BandId"]);

                if (dtPriceGuide.Rows[i]["ConditionId_2"] != DBNull.Value)
                    guide.ConditionId_2 = Convert.ToInt32(dtPriceGuide.Rows[i]["ConditionId_2"]);

                if (dtPriceGuide.Rows[i]["Factor"] != DBNull.Value)
                    guide.Factor = Convert.ToDouble(dtPriceGuide.Rows[i]["Factor"]);

                if (dtPriceGuide.Rows[i]["Var_A"] != DBNull.Value)
                    guide.VarA = Convert.ToDouble(dtPriceGuide.Rows[i]["Var_A"]);

                if (dtPriceGuide.Rows[i]["Var_B"] != DBNull.Value)
                    guide.VarB = Convert.ToDouble(dtPriceGuide.Rows[i]["Var_B"]);

                priceGuideColl.Add(guide);
            }

            Application["PriceBands"] = priceBandColl;
            Application["PriceGuides"] = priceGuideColl;
        }
    }
}